package com.ggbz.controller;


import com.ggbz.common.utils.JwtUtil;
import com.ggbz.common.utils.Result;
import com.ggbz.entity.CounselorClasses;
import com.ggbz.entity.Users;
import com.ggbz.entity.dto.CounselorClassUpdateRequest;
import com.ggbz.entity.dto.DeleteCounselorClassRequest;
import com.ggbz.service.CounselorClassesService;
import com.ggbz.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 辅导员负责班级表，存储辅导员与班级的关联关系 前端控制器
 * </p>
 *
 * @author ggbz
 * @since 2025-03-08
 */
@RestController
@RequestMapping("/counselor-classes")
public class CounselorClassesController {

    @Autowired
    private CounselorClassesService counselorClassesService;

    @Autowired
    private UsersService usersService;
    @GetMapping("/{counselorId}")
    public Result getCounselorClasses(@PathVariable Long counselorId) {
            // 3. 查询数据
            List<CounselorClasses> classes = counselorClassesService.getCounselorClasses(counselorId);
            return Result.ok().data("classes", classes);
    }

    @PostMapping
    public Result addCounselorClass(
            @RequestBody CounselorClasses request,
            @RequestHeader("Authorization") String token) {

        try {
            // 1. 解析Token并验证管理员权限
            String userId = (String) JwtUtil.getTokenBody(token).get("userId");
            Users currentUser = usersService.getById(userId);
            if (!"admin".equals(currentUser.getRole())) {
                return Result.error()
                        .message("需要管理员权限");
            }

            // 2. 执行业务逻辑
            counselorClassesService.addCounselorClass(request);
            return Result.ok()
                    .message("辅导员负责的班级添加成功");
        } catch (Exception e) {
            return Result.error()
                    .code(404)
                    .message(e.getMessage());
        }
    }

    @PutMapping
    public Result updateCounselorClass(
            @Valid @RequestBody CounselorClassUpdateRequest request,
            @RequestHeader("Authorization") String token) {

        try {
            // 权限验证
            String userId = (String) JwtUtil.getTokenBody(token).get("userId");
            Users currentUser = usersService.getById(userId);
            if (!"admin".equals(currentUser.getRole())) {
                return Result.error().code(403).message("需要管理员权限");
            }

            // 执行业务逻辑
            counselorClassesService.updateCounselorClass(request.getCounselorId(), request.getOldClassName(),request.getNewClassName());
            return Result.ok().message("班级信息更新成功");
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 删除辅导员负责的班级
     *
     * @param counselorId 辅导员ID
     * @param className   班级名称
     * @return 删除结果
     */
    @DeleteMapping()
    public Result deleteCounselorClass(@Valid @RequestBody DeleteCounselorClassRequest request) {
        // 调用业务层方法删除记录
        boolean result = counselorClassesService.deleteCounselorClass(request.getCounselorId(), request.getClassName());
        if (result) {
            // 返回删除成功的响应
            return Result.ok().message("辅导员负责的班级删除成功");
        } else {
            // 返回删除失败的响应
            return Result.error().message("删除辅导员负责的班级失败");
        }
    }
}

